using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace Starznet.SharePoint.SPDAT.ApplicationPages.Settings
{
    public class MembershipAddUserPage : LayoutsPageBase
    {
        protected InputFormSection IFSQandA;
        protected InputFormTextBox TxtFirstName;
        protected InputFormTextBox TxtSurname;
        protected InputFormTextBox TxtUsername;
        protected InputFormTextBox TxtPassword;
        protected InputFormTextBox TxtConfirmPassword;
        protected InputFormTextBox TxtQuestion;
        protected InputFormTextBox TxtAnswer;
        protected InputFormTextBox TxtEmail;
        protected PeopleEditor PeopleEditorRoles;
        protected PeopleEditor PeopleEditorSPGroups;
        protected Label AddUserError;
        protected CheckBox ChkAutoAddProfile;

        protected override void  OnLoad(EventArgs e)
        {
 	        base.OnLoad(e);

            IFSQandA.Visible = Membership.RequiresQuestionAndAnswer;
            PeopleEditorRoles.Enabled = Roles.Enabled;
        }

        protected void ValidatePassword(object sender, ServerValidateEventArgs args)
        {
            if (TxtPassword.Text != TxtConfirmPassword.Text)
                args.IsValid = false;
        }

        protected void BtnUpdateUser_Click(object sender, EventArgs e)
        {
            MembershipCreateStatus oStatus;

            string sQ = string.Empty;
            string sA = string.Empty;

            if (Membership.RequiresQuestionAndAnswer)
            {
                sQ = this.TxtQuestion.Text;
                sA = this.TxtAnswer.Text;
            }

            MembershipUser oUser = Membership.CreateUser(this.TxtUsername.Text,
                                  this.TxtPassword.Text,
                                  this.TxtEmail.Text,
                                  sQ,
                                  sA,
                                  true,
                                  out oStatus);

            //SPWeb oWeb = SPContext.Current.Site.RootWeb;

            if (oStatus == MembershipCreateStatus.Success)
            {
                //Add the user to the specified SharePoint group(s)
                foreach (PickerEntity oEntity in PeopleEditorSPGroups.Entities)
                {
                    SPGroup oGroup = SPContext.Current.Site.RootWeb.Groups[oEntity.Key];
                    oGroup.AddUser(oUser.ProviderName + ":" + oUser.UserName, oUser.Email, oUser.UserName, "");
                    oGroup.Update();
                }

                //Add the user to the specified membership role
                foreach (PickerEntity oEntity in PeopleEditorRoles.Entities)
                    Roles.AddUserToRole(oUser.UserName, oEntity.Key);

                //Automatically add the profile?
                //if (ChkAutoAddProfile.Checked)
                //{
                //    Microsoft.Office.Server.UserProfiles.UserProfileManager oUPMgr =
                //        new Microsoft.Office.Server.UserProfiles.UserProfileManager(true);

                //    Microsoft.Office.Server.UserProfiles.UserProfile oProfile = null;
                //    try
                //    { oProfile = oUPMgr.GetUserProfile(oUser.ProviderName + ":" + oUser.UserName); }
                //    catch
                //    { }

                //    if (oProfile == null)
                //    {
                //        oProfile = oUPMgr.CreateUserProfile(oUser.ProviderName + ":" + oUser.UserName);
                //        oProfile["FirstName"].Add(this.TxtFirstName.Text);
                //        oProfile["LastName"].Add(this.TxtSurname.Text);
                //        oProfile["PreferredName"].Add((string.IsNullOrEmpty(this.TxtFirstName.Text) || string.IsNullOrEmpty(this.TxtSurname.Text) ? oUser.UserName : this.TxtFirstName.Text + " " + this.TxtSurname.Text));
                //        oProfile["WorkEmail"].Add(this.TxtEmail.Text);
                //        oProfile.Commit();
                //    }
                //}
                Response.Redirect("MembershipUserList.aspx");
            }
            else
            {
                string sError = String.Empty;
                switch (oStatus)
                {
                    case MembershipCreateStatus.DuplicateUserName:
                        sError = this.GetGlobalResourceObject("szadm", "UserAddError_DuplicateUserName").ToString();
                        break;
                    case MembershipCreateStatus.InvalidAnswer:
                        sError = this.GetGlobalResourceObject("szadm", "UserAddError_PassReminder").ToString();
                        break;
                    case MembershipCreateStatus.DuplicateEmail:
                        sError = this.GetGlobalResourceObject("szadm", "UserAddError_DuplicateEmail").ToString();
                        break;
                    case MembershipCreateStatus.InvalidPassword:
                        sError = this.GetGlobalResourceObject("szadm", "UserAddError_InvalidPassword").ToString();
                        break;
                }

                AddUserError.Visible = true;
                AddUserError.Text = sError;
            }
        }

        private void Rollback(MembershipUser User)
        {

        }
    }
}
